/*****************************************************************************
 * main.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"
#include "dev_mysql.h"
#include "dev_weight.h"
/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL   LOG_LEVEL_DEBUG
#define LOG_MODULE      "dev.main"
#include "thirdparty/log/log.h" 
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/
#define DEV_MYSQL_HOST  "rm-2vcu9uruhole55131so.mysql.cn-chengdu.rds.aliyuncs.com"
#define DEV_MYSQL_PORT  3306
#define DEV_MYSQL_USR   "cdwptest"
#define DEV_MYSQL_PASSWD"cdwp$1234"
#define DEV_MYSQL_DB    "test_hs"

#define DEV_SENSOR_UART_ID  PLUM_HAL_UART_ID(100)
/*****************************************************************************
 * Private Types
 ****************************************************************************/

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/
PLUM_PRIVATE    
plum_u32    dev_sensor_uart_id;
/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
plum_s32    dev_sensor_write(plum_u8 *wData,plum_u32 cnt)
{
    return plum_hal_uart_send(DEV_SENSOR_UART_ID,wData,cnt);
}

PLUM_PRIVATE
plum_s32    dev_sensor_read(plum_u8 *rData,plum_u32 cnt,plum_u32 timeout)
{
    return plum_hal_uart_recv(DEV_SENSOR_UART_ID,rData,cnt,timeout);
}
/*****************************************************************************
 * Public Functions
 ****************************************************************************/
void    main(int argc,char **argv)
{
    if(argc != 2){
        LOG_E("the arg is err,nedd [uart_id] parameter");
        LOG_E("exit ");
        return;
    }

    plum_s32 rc;

    rc = plum_cal_cli_init(getchar);
    if(rc){
        LOG_E("plum_cal_cli_init err,rc : %d",rc);
    }
#if 0
    dev_mysql_hdl_t hdl = dev_mysql_connect(DEV_MYSQL_HOST,DEV_MYSQL_PORT,
                                DEV_MYSQL_USR,DEV_MYSQL_PASSWD,
                                DEV_MYSQL_DB);
    if(!hdl){
        LOG_E("dev_mysql_connect err");
    }
#else
    plum_hal_uart_cof_t uart = {
        .baudrate = 115200,
        .databit = PLUM_HAL_UART_DATABIT_8,
        .flowctrl = PLUM_HAL_UART_FLOW_CTRL_NONE,
        .gpio.rx = PLUM_HAL_GPIO_ID(A,1),
        .gpio.tx = PLUM_HAL_GPIO_ID(A,2),
        .stopbit = PLUM_HAL_UART_STOPBIT_1,
        .rxbuf_size = 256,
        .parity = PLUM_HAL_UART_PARITY_NONE,
    };
    rc = plum_hal_uart_init(DEV_SENSOR_UART_ID,&uart);
    if(rc){
        LOG_E("plum_hal_uart_init err,rc : %d",rc);
    }

    rc = dev_weight_protocol_register(PROTOCOL_VERSION_V21,dev_sensor_write,dev_sensor_read);
    if(rc){
        LOG_E("dev_weight_protocol_register err,rc : %d",rc);
    }
#endif

    while(1){
        plum_kal_thread_delay(100);
    }
}

/*****************************************************************************
 * Cli Functions
 ****************************************************************************/

#include "thirdparty/transcode/transcode.h"

plum_void   cli_main_uart_write(plum_u32 argc,plum_s8 **argv)
{
    plum_s8 *cmd_str = argv[0];
    plum_u32   cmd_len = strlen(argv[0]) / 2;

    plum_u8 *cmd = malloc(cmd_len);
    if(cmd){
        memset(cmd,0,cmd_len);
    }

    str2num(cmd_str,cmd,cmd_len);
    LOG_IHEX("write cmd : ",cmd,cmd_len);
  //  plum_hal_uart_send(DEV_SENSOR_UART_ID,cmd,cmd_len);

    free(cmd);
}
PLUM_CAL_CLI_EXPORT("write_cmd","write cmd by uart",cli_main_uart_write);

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
